package com.ma.DataStruct.twitter;

import java.util.*;


public class Solution355 {
}

/*
    设计推特类 面向对象设计

 */
class Twitter {
    //时间戳
    private static int timestamp = 0;

    // 推特类的设计  推特id,推特对应的时间戳,作为链表节点,要有指向下一个节点的next指针
    private  class Tweet {
        private int id;
        private int timestamp;
        private Tweet next;

        // 需要传入推文内容（id）和发文时间
        public Tweet(int id, int time) {
            this.id = id;
            this.timestamp = time;
            this.next = null;
        }
    }

    //用户类的设计
    private  class User {
        private int id;
        public Set<Integer> followed;
        public Tweet head;

        public User(int userId) {
            followed = new HashSet<>();
            this.id = userId;
            this.head = null;
            follow(id);
        }

        //关注某个用户
        public void follow(int userId) {
            followed.add(userId);
        }

        //取关某个用户
        public void unfollow(int userId) {
            if (this.id != userId) {
                followed.remove(userId);
            }
        }

        //发表某条推特
        public void post(int tweetId) {
            Tweet tweet = new Tweet(tweetId, timestamp);
            timestamp++;
            //插入链表头
            tweet.next = head;
            head = tweet;
        }
    }

    //userId与User的映射
    private HashMap<Integer, User> userMap = new HashMap<>();


    /**
     * 发表一条推特
     */
    public void postTweet(int userId, int tweetId) {
        //userId 不存在，新建USER
        if (!userMap.containsKey(userId)) {
            userMap.put(userId, new User(userId));
        }
        User user = userMap.get(userId);
        user.post(tweetId);
    }

    /**
     * follower 关注 followee
     */
    public void follow(int followerId, int followeeId) {
        if (!userMap.containsKey(followerId)) {
            User user = new User(followerId);
            userMap.put(followerId, user);
        }
        if (!userMap.containsKey(followeeId)) {
            User user = new User(followeeId);
            userMap.put(followeeId, user);
        }
        userMap.get(followerId).follow(followeeId);
    }

    /**
     * follower 取消关注 followee 如果follower id不存在 什么都不做
     */
    public void unfollow(int followerId, int followeeId) {
        if (userMap.containsKey(followerId) && userMap.containsKey(followeeId)) {
            User follower = userMap.get(followerId);
            follower.unfollow(followeeId);
        }
    }

    /**
     * 返回该User关注的人（包括他自己）最近的动态id 最多十条 按时间从新到旧排列
     */
    public List<Integer> getNewsFeed(int userId) {
        List<Integer> res = new ArrayList<>();
        if (!userMap.containsKey(userId)) {
            return res;
        }
        Set<Integer> users = userMap.get(userId).followed;
        PriorityQueue<Tweet> pq = new PriorityQueue<>(users.size(), (a, b) -> b.timestamp - a.timestamp);
        //将所有链表的头节点插入优先级队列
        for (int id : users) {
            Tweet twt = userMap.get(id).head;
            if (twt == null) {
                continue;
            }
            pq.add(twt);
        }

        while (!pq.isEmpty()) {
            //最多返回十条
            if (res.size() == 10) {
                break;
            }
            //
            Tweet twt = pq.poll();
            res.add(twt.id);
            if (twt.next != null) {
                pq.add(twt.next);
            }
        }
        return res;
    }


}


